package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DUserService;
import cn.huiyunche.base.service.mappers.DUserMapper;
import cn.huiyunche.base.service.model.DUser;
import cn.huiyunche.base.service.model.DUserExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jxy on 16/12/12.
 */
@Service
public class DUserServiceImpl implements DUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DUserServiceImpl.class);

    @Autowired
    private DUserMapper dUserMapper;

    public int insertSelective(DUser dUser) {
        return dUserMapper.insertSelective(dUser);
    }

    public int deleteByPrimaryKey(Long id) {
        return dUserMapper.deleteByPrimaryKey(id);
    }


    public DUser selectByPrimaryKey(Long id) {
        return dUserMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(DUser record) {
        return dUserMapper.updateByPrimaryKeySelective(record);
    }

    public boolean validPhoneExist(String phone) {
        DUserExample dUserExample = new DUserExample();
        dUserExample.createCriteria().andLoginNameEqualTo(phone);
        List<DUser> list = dUserMapper.selectByExample(dUserExample);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public DUser selectByPhone(String phone) {
        LOGGER.info("DUserServiceImpl.selectByPhone param : {}", phone);

        if (StringUtils.isBlank(phone)) {
            LOGGER.error("DUserServiceImpl.selectByPhone param phone must not be null");
            throw new IllegalArgumentException("手机号码不能为空");
        }

        DUserExample dUserExample = new DUserExample();
        dUserExample.createCriteria().andLoginNameEqualTo(phone);

        List<DUser> list = dUserMapper.selectByExample(dUserExample);

        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }
}
